﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbMapping.Common;
using VideoCache.Server.Model;

namespace VideoCache.Server.BLL.EpisodeFetcher
{
    class YoukuEpisodeFetcher : IEpisodeFetcher
    {
        public EpisodeInfo GetEpisodeByUrl(string url)
        {
            if (url.IndexOf("?") > -1)
            {
                string findUrl = url.Substring(0, url.IndexOf("?"));

                SiteInfo info = this.GetSite();
                if (info != null)
                {
                    Conditions cons = new Conditions();
                    cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.SiteIdField, CompareType.Equal, info.Id));
                    cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.UrlField, CompareType.Equal, findUrl));

                    return EpisodeBLL.Find(cons, null);
                }
            }

            return null;
        }

        public EpisodeInfo GetEpisodeByShareUrl(string url)
        {
            // 优酷分享链接格式如下
            // http://static.youku.com/v1.0.0317/v/swf/loader.swf?VideoIDS=XNTQyNzY0NDk2&ShowId=0&category=91&Cp=0&Light=on&THX=off&unCookie=0&frame=0&pvid=13660096162218Mu&Tid=0&isAutoPlay=true&Version=/v1.0.0849&show_ce=0&winType=interior&embedid=AjEzNTY5MTEyNAJuZXdzLnlvdWt1LmNvbQIvcWlubGl1Z2FuMjAxMy9pbmRleA==&vext=bc%3D%26pid%3D%26unCookie%3D0%26frame%3D0%26type%3D0%26svt%3D0%26emb%3DAjEzNTY5MTEyNAJuZXdzLnlvdWt1LmNvbQIvcWlubGl1Z2FuMjAxMy9pbmRleA%3D%3D%26dn%3D%E7%BD%91%E9%A1%B5%26hwc%3D1%26mtype%3Doth

            Uri uri;
            if (Uri.TryCreate(url, UriKind.Absolute, out uri))
            {
                if (uri.PathAndQuery.IndexOf("?") > -1)
                {
                    string query = uri.PathAndQuery.Substring(uri.PathAndQuery.IndexOf("?") + 1);

                    foreach (string item in query.Split('&'))
                    {
                        if (item.ToLower().StartsWith("videoids="))
                        {
                            string token = item.Substring(item.IndexOf("=") + 1);

                            SiteInfo info = this.GetSite();

                            if (info != null)
                            {
                                Conditions cons = new Conditions();
                                cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.SiteIdField, CompareType.Equal, info.Id));
                                cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.TokenField, CompareType.Equal, token));

                                return EpisodeBLL.Find(cons, null);
                            }
                        }
                    }
                }
            }
            else
            {
                throw new ArgumentException("错误的Url: " + url);
            }

            return null;
        }

        private SiteInfo GetSite()
        {
            Conditions cons = new Conditions();
            cons.Add(new ConditionItem(LinkType.And, SiteInfo.UrlHostField, CompareType.Equal, "youku.com"));

            return SiteBLL.Find(cons, null);
        }
    }
}